package com.da.lon.wang.xlg.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.WaitGoods;
import com.da.lon.wang.xlg.util.CustomListener;
import com.da.lon.wang.xlg.util.ViewHolder;
import com.da.lon.wang.xlg.view.RoundImageView;

import java.util.List;

public class WaitGoosAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<WaitGoods> goods;
    private Activity context;
    private CustomListener listener;

    public WaitGoosAdapter(List<WaitGoods> list, Activity context,
                           CustomListener listener) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.listener = listener;
        this.goods = list;
    }

    @Override
    public int getCount() {
        return goods != null ? goods.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return goods != null && goods.size() > position ? goods.get(position)
                : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {
            if (goods != null && goods.size() > position) {
                final WaitGoods good = goods.get(position);

                convertView = inflater.inflate(
                        R.layout.ouka_seller_wait_listviewitem, null);

                TextView nameTextView = ViewHolder.get(convertView,
                        R.id.wait_name);
                TextView quantityTextView = ViewHolder.get(convertView,
                        R.id.wait_quantity);
                TextView weightTextView = ViewHolder.get(convertView,
                        R.id.wait_weight);
                TextView priceTextView = ViewHolder.get(convertView,
                        R.id.wait_price);
                TextView dayTextView = ViewHolder.get(convertView,
                        R.id.wait_day);
                TextView hoursTextView = ViewHolder.get(convertView,
                        R.id.wait_hours);
                TextView addressTextView = ViewHolder.get(convertView,
                        R.id.wait_address);
                TextView m_grap = ViewHolder.get(convertView, R.id.m_grap);

                if (good != null && null != good.getOrder_product()
                        && good.getOrder_product().size() > 0) {
                    nameTextView.setText(good.getOrder_product().get(0)
                            .getProducts_name());
                    quantityTextView.setText(good.getOrder_product().get(0)
                            .getProducts_quantity());
                    priceTextView.setText(good.getOrders_total());

                    if (good.getOrder_product().get(0).getWeight() == null
                            || good.getOrder_product().get(0).getWeight()
                            .equals("")) {
                        weightTextView.setVisibility(View.GONE);
                    } else {
                        weightTextView.setText("("
                                + good.getOrder_product().get(0).getWeight()
                                + ")");
                    }

                    ImageView waitImageView = ViewHolder.get(convertView,
                            R.id.wait_image);
                    waitImageView.setScaleType(ScaleType.FIT_CENTER);
                    if (good.getOrder_product().get(0).getBigimage() != null
                            && !"".equals(good.getOrder_product().get(0)
                            .getBigimage()) && waitImageView != null)
                        MyApplication.loadImageByPicasso(context, good
                                .getOrder_product().get(0).getBigimage(), null, waitImageView, 0, 0, 0);
//						MyApplication.loadImage(context, good
//								.getOrder_product().get(0).getBigimage(),
//								waitImageView);
                }

                dayTextView.setText(good.getDay());
                hoursTextView.setText(good.getHours());
                addressTextView.setText(good.getAddress());

                RoundImageView photoImageView = ViewHolder.get(convertView,
                        R.id.wait_photo);
                if (photoImageView != null && good.getHead_log() != null
                        && !"".equals(good.getHead_log())) {
                    MyApplication.loadImageByPicasso(context, good.getHead_log(), null, photoImageView, 480, 480, 0);
//					MyApplication.loadImageAndResize(context,
//							good.getHead_log(), photoImageView, 480, 480);
                }
                m_grap.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        if (listener != null)
                            listener.onCustomListener(0, good, position);// 抢单
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }
}
